/**
 * 列表页通用方法
 */

layui.define(["miniPage", "jquery","table",'form'], function (exports) {
    var miniPage = layui.miniPage,
        $ = layui.$,
        table = layui.table,
        form = layui.form,
        layer = layui.layer;

    var miniIndex = {

        /**
         * 表格工具条
         * @param url
         */
        tableEvent : function (url) {
            //监听工具条
            table.on('tool(table)', function (obj) {
                var data = obj.data;
                var layEvent = obj.event;
                console.log(layEvent)
                switch (layEvent) {
                    case 'del':
                        layer.confirm('真的删除这个吗', function (index) {
                            layer.load(2);
                            $.ajax({
                                url: url + '/' + data.id,
                                method: 'DELETE',
                                success: function (res) {
                                    layer.msg(res.msg,function () {
                                        obj.del();
                                        layer.closeAll('loading');
                                        layer.close(index);
                                    })
                                }
                            })
                        });
                        break;
                    case 'edit':
                        layer.closeAll('loading');
                        var content = miniPage.getHrefContent(url+'/'+data.id+'/edit');
                        var openWH = miniPage.getOpenWidthHeight();
                        var edit_view = layer.open({
                            title: '修改'+url,
                            type: 1,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: [openWH[0] + 'px', openWH[1] + 'px'],
                            offset: [openWH[2] + 'px', openWH[3] + 'px'],
                            content: content,
                        });
                        $(window).on("resize", function () {
                            layer.full(edit_view);
                        });
                        break;
                    case 'show' :
                        var contentShow = miniPage.getHrefContent(url+'/'+data.id+'/show');
                        var openWH = miniPage.getOpenWidthHeight();
                        var edit_view = layer.open({
                            title: '修改'+url,
                            type: 1,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: [openWH[0] + 'px',  '550px'],
                            offset: ['200px', openWH[3] + 'px'],
                            content: contentShow,
                        });
                        $(window).on("resize", function () {
                            layer.full(edit_view);
                        });
                }
            });
        },

        /**
         * toolbar事件监听
         *
         * @param elem
         * @param url
         * @constructor
         */
        ToolbarEvent:function(elem, url){
            table.on('toolbar('+ elem +')', function (obj) {
                if (obj.event === 'add') {   // 监听添加操作
                    var content = miniPage.getHrefContent(url);
                    var openWH = miniPage.getOpenWidthHeight();

                    var index = layer.open({
                        title: '添加',
                        type: 1,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: [openWH[0] + 'px', openWH[1] + 'px'],
                        offset: [openWH[2] + 'px', openWH[3] + 'px'],
                        content: content,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            });
        },

        /**
         * 表格复选框切换
         *
         * @param obj 刷新table对象
         * @param url 请求url
         * @param filter lay-filter 名
         */
        tableCheckbox : function (obj, url, filter) {
            form.on("checkbox(" + filter + ")" , function () {
                miniIndex.changeState(obj, url, filter, this.value)
            })
        },

        /**
         *  表格选项框
         *
         * @param obj 刷新table对象
         * @param url 请求url
         * @param filter lay-filter 名
         */
        tableSwitch : function(obj, url, filter){
            form.on("switch("+ filter +")",function(){
                console.log(this.value)
                miniIndex.changeState(obj, url, filter , this.value);
            })
        },

        /**
         * 修改状态
         *
         * @param obj 刷新table对象
         * @param url 请求url
         * @param field 字段
         * @param id 修改id
         */
        changeState : function (obj, url, field, id) {
            $.ajax({
                url: '/admin/change_state/'+ url + '/' + field,
                data: {id:id},
                type: "post",
                dataType: "json",
                headers:{'X-CSRF-TOKEN' : $('meta[name = "csrf-token"]').attr('content')},
                success: function (res) {
                    if (res.code === 0){
                        layer.msg(res.msg,{icon:1,time:2000},function () {
                            obj.reload();
                        });
                    } else {
                        layer.msg(res.msg,{icon:5,time:2000},function () {
                            obj.reload();
                        })
                    }
                }
            })
        }
    };

    exports("miniIndex", miniIndex);
});
